package web.servlet;

import domain.Account;
import domain.Cart;
import domain.Order;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.OrderService;

import java.io.IOException;
import java.io.PrintWriter;

public class NewOrderServlet extends HttpServlet {
    //将所有的订单信息保存到session的currentOrder中
    //跳转到shipping或confirm页面
    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String SHIPPING_ORDER_FORM = "/WEB-INF/jsp/order/ShippingForm.jsp";
    String check;

    Order order;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //newOrder页面数据
        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("loginAccount");
        Cart cart = (Cart) session.getAttribute("cart");
        order = new Order();
        order.initOrder(account,cart);

        String cardType = req.getParameter("cardType");
        String creditCard = req.getParameter("creditCard");
        String expiryDate = req.getParameter("expiryDate");
        String billToFirstName = req.getParameter("billToFirstName");
        String billToLastName = req.getParameter("billToLastName");
        String billAddress1 = req.getParameter("billAddress1");
        String billAddress2 = req.getParameter("billAddress2");
        String billCity = req.getParameter("billCity");
        String billState = req.getParameter("billState");
        String billZip = req.getParameter("billZip");
        String billCountry = req.getParameter("billCountry");

        String shipToFirstName = req.getParameter("shipToFirstName");
        String shipToLastName = req.getParameter("shipToLastName");
        String shipAddress1 = req.getParameter("shipAddress1");
        String shipAddress2 = req.getParameter("shipAddress2");
        String shipCity = req.getParameter("shipCity");
        String shipState = req.getParameter("shipState");
        String shipZip = req.getParameter("shipZip");
        String shipCountry = req.getParameter("shipCountry");

//
        if (cardType != null && !cardType.equals("")) {
            order.setCardType(cardType);
        }
        if (creditCard != null && !creditCard.equals("")) {
            order.setCreditCard(creditCard);
        }
        if (expiryDate != null && !expiryDate.equals("")) {
            order.setExpiryDate(expiryDate);
        }
        if (billToFirstName != null && !billToFirstName.equals("")) {
            order.setBillToFirstName(billToFirstName);
        }
        if (billToLastName != null && !billToLastName.equals("")) {
            order.setBillToLastName(billToLastName);
        }
        if (billAddress1 != null && !billAddress1.equals("")) {
            order.setBillAddress1(billAddress1);
        }
        if (billAddress2 != null && !billAddress2.equals("")) {
            order.setBillAddress2(billAddress2);
        }
        if (billCity != null && !billCity.equals("")) {
            order.setBillCity(billCity);
        }
        if (billState != null && !billState.equals("")) {
            order.setBillState(billState);
        }
        if (billZip != null && !billZip.equals("")) {
            order.setBillZip(billZip);
        }
        if (billCountry != null && !billCountry.equals("")) {
            order.setBillCountry(billCountry);
        }

        session.setAttribute("currentOrder", order);

//
        check = req.getParameter("shippingAddressRequired");

        PrintWriter out = resp.getWriter();
        if(check == null){
            out.print("not checked");
        } else {
            out.print("checked");
        }
//        if (check==null){
//            //如果未点击ship to different则跳转到confirm页面,在confirm页面显示Order时间,BillingAddress(当前页面信息)和ShippingAddress(默认用户信息)的信息
//            req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req,resp);
//        }else {
//            req.getRequestDispatcher(SHIPPING_ORDER_FORM).forward(req,resp);
//        }
//        HttpSession session = req.getSession();
//        Order order =(Order) session.getAttribute("order");
//        order.setUsername(username);
//        req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req,resp);
    }
}
